<template>
	<view class="user-info">
		<view class="info-header">
			<view class="title">管理我的账号</view>
			<view class="account" @click="chooseImage">
				<view class="user-avatar">
					<!-- <image v-if="userInfo.head_portrait" :src="userAvatar"/> -->
					<image v-if="userInfo.head_portrait" :src="userInfo.head_portrait"/>
					<image v-else :src="userAvatar"/>
				</view>
				<view class="other">
					<view class="user-name t-o-e">{{userInfo.nickname}}</view>
					<view class="user-phone">{{userInfo.username}}</view>
				</view>
				<view class="float-box">
					<view class="box-text">当前账号</view>
				</view>
			</view>
		</view>
		<view class="info-body">
			<view class="info-list">
				<view class="list-item">
					<view class="item-left">昵称</view>
					<view class="item-right">
						<input class="nickname-input" placeholder="请输入昵称" :maxlength="16" :value="userInfo.nickname" @input="changeForm($event, 'nickname')" />
					</view>
				</view>
				<view class="h-line"/>
				<view class="list-item">
					<view class="item-left">手机号</view>
					<view class="item-right">{{userInfo.username}}</view>
				</view>
				<view class="h-line"/>
				<view class="list-item">
					<view class="item-left">ID号</view>
					<view class="item-right">{{userInfo.id}}</view>
				</view>
			</view>
		</view>
		<view class="info-footer">
			<slw-button circle @click="formSubmit">保存修改</slw-button>
		</view>
	</view>
</template>

<script>
	import {setLoginInfo} from '@/utils/storage.js'
	import {siteLogin} from '@/api/site.js'
	import {updateHeadImg, updateUserInfo} from '@/api/member.js'
	import {MEMBER_INFO} from '@/config/storage.js'
	import {SERVICE_URL} from '@/config/index.js'
	import {toUserPage} from '@/utils/PageRouter.js'
	import SlwButton from '@/components/slw-ui/button/index.vue'
	import userAvatar from '@/static/images/user-avatar.png'
	
	export default {
		components: {SlwButton},
		data() {
			return {
				form: {},
				userInfo: {},
				userAvatar
			}
		},
		onLoad() {
			this.getUserInfo()
		},
		methods: {
			//	获取用户信息
			getUserInfo () {
				const userInfo = uni.getStorageSync(MEMBER_INFO)
				this.userInfo = userInfo
			},
			//	输入框 change 事件
			changeForm (e, name) {
				const {value} = e.target
				this.$set(this.form, name, value)
			},
			//	提交表单数据
			formSubmit () {
				const {form} = this
				uni.showLoading({title: '加载中...'})
				updateUserInfo(form).then(res => {
					uni.hideLoading()
					uni.showToast({title: '修改成功', icon: 'none'})
					toUserPage()
				}).catch(() => {
					uni.hideLoading()
					uni.showToast({title: '修改成功', icon: 'none'})
				})
			},
			//	上传图片
			chooseImage () {
				uni.showLoading({title: '加载中...'})
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album'],
					success: (res) => {
						let imageSrc = res.tempFilePaths[0]
						let image = res.tempFiles[0]
						updateHeadImg(imageSrc).then(res => {
							// const {userInfo} = this
							// uni.setStorageSync(MEMBER_INFO, userInfo)
							uni.hideLoading()
							uni.showToast({title: '修改成功', icon: 'none'})
							toUserPage()
						}).catch(res => {
							uni.hideLoading()
							uni.showToast({title: '修改失败', icon: 'none'})
						})
					},
					fail: (err) => {
						uni.hideLoading()
						uni.showToast({title: '文件错误', icon: 'none'})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.user-info{
		.info-header{
			padding: 20rpx;
			background-color: #fff;
			margin-bottom: 20rpx;
			.title{
				padding: 20rpx 0;
			}
			.account{
				border: 2rpx solid $theme_color;
				display: flex;
				align-items: center;
				overflow: hidden;
				position: relative;
				border-radius: 24rpx;
				background-color: #94C1FB;
				.user-avatar{
					width: 100rpx;
					height: 100rpx;
					margin: 20rpx;
					overflow: hidden;
					border-radius: 50%;
				}
				.user-name{
					width: 300rpx;
					font-size: 32rpx;
					margin-bottom: 10rpx;
				}
				.user-phone{
					font-size: 24rpx;
				}
				.float-box{
					top: 0;
					right: 0;
					position: absolute;
					.box-text{
						padding: 8rpx 16rpx;
						border-radius: 0 0 0 24rpx;
						background-color: #fce9e5;
					}
				}
			}
		}
		.info-body{
			.info-list{
			}
			.list-item{
				display: flex;
				padding: 30rpx 20rpx;
				justify-content: space-between;
				background-color: #fff;
				.item-left{
					font-size: 32rpx;
				}
				.nickname-input{
					text-align: right;
				}
			}
		}
		.info-footer{
			padding: 50rpx 20rpx 0;
		}
	}
</style>
